/*
     Copyright 2007 Joseph M. Ferner

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

using System;
using System.Reflection;

namespace OpenServers.Net.Common.Utils {
    /// <summary>
    /// a wrapper around a type for calling reflection methods on that object.
    /// </summary>
    public class PrivateStaticObject {
        private readonly Type _wrappedType;

        /// <summary>
        /// constructor.
        /// </summary>
        public PrivateStaticObject(Type wrappedType) {
            this._wrappedType = wrappedType;
        }

        /// <summary>
        /// The wrapped type.
        /// </summary>
        public Type WrappedType {
            get { return _wrappedType; }
        }

        /// <summary>
        /// Gets a method.
        /// </summary>
        public MethodInfo GetMethod(string methodName, Type[] types) {
            return ReflectionUtils.GetMethod(WrappedType, methodName, types);
        }

        /// <summary>
        /// gets all the methods with the given name.
        /// </summary>
        public MethodInfo[] GetMethods(string methodName) {
            return ReflectionUtils.GetMethods(WrappedType, methodName);
        }

        /// <summary>
        /// gets all the methods with the given name, and parameter count.
        /// </summary>
        public MethodInfo[] GetMethods(string methodName, int parameterCount) {
            return ReflectionUtils.GetMethods(WrappedType, methodName, parameterCount);
        }

        /// <summary>
        /// sets a static field value.
        /// </summary>
        public void SetStaticFieldValue(string fieldName, object value) {
            FieldInfo fieldInfo = GetStaticField(fieldName);
            if (fieldInfo == null) {
                throw new FieldAccessException(string.Format("field [{0}] not found", fieldName));
            }
            fieldInfo.SetValue(null, value);
        }

        /// <summary>
        /// gets a static field.
        /// </summary>
        private FieldInfo GetStaticField(string fieldName) {
            return ReflectionUtils.GetStaticField(WrappedType, fieldName);
        }
    }
}
